package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.Slice;
import com.ffcs.crmd.tsp.admin.service.ISliceService;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.api.dto.client.TspSlice;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
@Service
public class SliceServiceImpl extends AbstractService implements ISliceService{
    
    @Override
    public void deleteSlice(List<Slice> sliceList) throws TspException {
        for(Slice slice : sliceList){
            deleteSlice(slice);
        }
    }
    
    
    @Override
    public void deleteSlice(Slice slice) throws TspException {
        try {
            TspSlice tspSliceVO = new TspSlice();
            tspSliceVO.setId(slice.getId());
            tspSliceVO.setClusterName(slice.getClusterName());
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<?> tspResult = tspClient.deleteSlice(tspSliceVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public Slice get(Slice slice) throws TspException {
        try {
            TspSlice tspSliceVO = new TspSlice();
            BeanUtils.applyIf(tspSliceVO, slice, false);
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<TspSlice> tspResult = tspClient.querySlice(tspSliceVO);
            validateResult(tspResult);
            
            Slice rslice = new Slice();
            BeanUtils.applyIf(rslice, tspResult.getRetVo().getObject(), false);
            return rslice;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public List<Slice> query(Slice slice) throws TspException {
        try {
            TspSlice tspSliceVO = new TspSlice();
            BeanUtils.applyIf(tspSliceVO, slice, false);
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<TspSlice> tspResult = tspClient.querySlice(tspSliceVO);
            validateResult(tspResult);
            
            List<TspSlice> tspSliceVOList = new ArrayList<TspSlice>();
            tspSliceVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspSliceVOList, Slice.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public PageInfo<Slice> queryPage(Slice slice, int pageNumber, int pageSize) throws TspException {
        try {
            TspSlice tspSliceVO = new TspSlice();
            BeanUtils.applyIf(tspSliceVO, slice, false);
            tspSliceVO.setPageNumber(pageNumber);
            tspSliceVO.setPageSize(pageSize);
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<TspSlice> tspResult = tspClient.querySlice(tspSliceVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspSlice> tspSlicePageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<Slice> slicePageInfo = new PageInfo<Slice>();
            BeanUtils.applyIf(slicePageInfo, tspSlicePageInfo, false);
            slicePageInfo.setList(BeanUtils.copyList(tspSlicePageInfo.getList(), Slice.class));
            return slicePageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void saveSlice(Slice slice) throws TspException{
        try {
            TspSlice tspSliceVO = new TspSlice();
            BeanUtils.applyIf(tspSliceVO, slice);
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<?> tspResult = tspClient.createSlice(tspSliceVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void updateSlice(Slice slice) throws TspException{
        try {
            TspSlice tspSliceVO = new TspSlice();
            BeanUtils.applyIf(tspSliceVO, slice);
            
            TspClient tspClient = this.getClient(slice.getClusterName());
            TspResult<?> tspResult = tspClient.updateSlice(tspSliceVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }


}
